#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include<unistd.h>
#include <pty.h>
#include <sys/stat.h>

#include <gtk/gtk.h>

#include "wlterm_session.h"

WltermSession * wlterm_session_create(WltermSite *site)
{
	WltermSession *thiz = NULL;
	gint i;
	thiz = g_malloc(sizeof(WltermSession));
	if (thiz == NULL)
		return NULL;
	thiz->screen = g_malloc(sizeof(WltermData*) * site->rows);
	if (thiz->screen == NULL)
		return NULL;
	for (i = 0; i < site->rows; i++) {
		thiz->screen[i] = g_malloc0(sizeof(WltermData) * site->cols);
		if (thiz->screen[i] == NULL)
			return NULL;
	}
	thiz->x = 0;
	thiz->y = 0;
	thiz->site = site;

	return thiz;
}

void wlterm_session_destroy(WltermSession * thiz)
{
	gint i;
	for (i = 0; i < thiz->site->rows; i++) {
		g_free(thiz->screen[i]);
	}
	g_free(thiz->screen);
	g_free(thiz);
}

gboolean session_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
{
	if (condition & G_IO_IN) {
		gchar buf[BUF_LEN];
		gsize rsize;
		GError *r_err;
		g_io_channel_read_chars(source, buf, BUF_LEN, &rsize, NULL);
		gint i = 0;
		buf[rsize] = '\0';
		WltermSession *thiz = (WltermSession*)data;
		wlterm_parse_data(thiz, buf, rsize); 
	}
	return TRUE;
}

void wlterm_session_connect(WltermSession *thiz)
{
	int s_pid;
	int s_fd;
	WltermSite *site;
	site = thiz->site;
	s_pid = forkpty(&s_fd, NULL, NULL, NULL);
	if (s_pid == 0) {
		close(s_fd);
		if (site->login_type == 0) { /* telnet */
			execlp(TELNET_CMD, TELNET_CMD, "-8", site->address, NULL);
		} else if (site->login_type == 1) { /* ssh */
			execlp(SSH_CMD, "-l", site->username, "-p", site->port,
					site->address);
		}
		exit(EXIT_FAILURE);
	} else {
		//parent process
		GIOChannel *s_io;
		guint s_ioid;
		s_io = g_io_channel_unix_new(s_fd);
		s_ioid = g_io_add_watch(s_io, 
				(GIOCondition)(G_IO_ERR|G_IO_HUP|G_IO_IN),
				(GIOFunc)session_io_func,
				thiz);
		g_io_channel_set_encoding(s_io, NULL, NULL);
		g_io_channel_set_buffered(s_io, FALSE);
	}
}

#if 0
main()
{
	WltermSession *session;
	WltermSite site = {
		.address = "bbs.seu.edu.cn",
		.port = 23,
		.login_type = TELNET,
		.rows = SCREEN_ROWS,
		.cols = SCREEN_COLS,
		.encoding = "GBK"
	};
	session = wlterm_session_create(&site);


#if 0
	/* read the control sequence data to str*/
	gchar *str;
	gint len;
	struct stat statbuf;
	stat("data", &statbuf);
	len = statbuf.st_size/sizeof(gchar);
	str = g_malloc(sizeof(gchar) * len);
	if (str == NULL)
		return -1;

	FILE *fp = NULL;
	fp = fopen("data", "r");
	if (fp == NULL)
		return -2;
	fread(str, sizeof(gchar), len, fp);
	fclose(fp);
	/* end read */
#if 0
	gchar *res;
	res = g_convert(str, len, "UTF-8", "GBK", NULL, NULL, NULL);
	wlterm_data_parse(session, res, len);
#endif
	wlterm_data_parse(session, str, len);

	gint i, j;
	gchar line[81];
	gchar nl[2] = "\n";
	line[80] = '\0';
	printf("%s\n", str);
	printf("current row:%d col:%d\n", session->x, session->y);
#if 1
	for (i = 0; i < session->site->rows; i++) {
#endif
#if 0
	for (i = 0; i < 10; i++) {
#endif
		for (j = 0; j < session->site->cols; j++) {
			printf("%d:%d %c\n", i, j, session->screen[i][j].ch);
			line[j] = session->screen[i][j].ch;
		}
		//fwrite(line, sizeof(gchar), 81, fp);
	}
#endif
	


	wlterm_session_connect(session);
	GMainLoop *mloop = NULL;
	mloop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(mloop);
	wlterm_session_destroy(session);
	return 0;
}
#endif
